﻿//#define TEST_SDK

#if (!UNITY_EDITOR || TEST_SDK) && WEIXINMINIGAME

using Cysharp.Threading.Tasks;
using WeChatWASM;

namespace KuiHuaBaoDian.SDK {

    public static partial class API {

        private partial class Implementation {

            public async UniTask<IAPI.ILoginResult> Login() {
                var loginResult = new LoginResult();
                //feature.AddShare(m_FeatureSetting.Share);
                //feature.AddSideBar(m_FeatureSetting.SideBar);
                //feature.AddDesktop(m_FeatureSetting.Desktop);
                //feature.AddGameClub(m_FeatureSetting.GameClub);
                //feature.AddMiniProgram(m_FeatureSetting.AddMiniProgram);
                //feature.AddSubscribe(m_FeatureSetting.Subscribe);

                var accountInfo = WX.GetAccountInfoSync();
                var envVersion = accountInfo == null ? "trial" : accountInfo.miniProgram.envVersion;
                if (envVersion == "release") {
                    loginResult.Version = accountInfo.miniProgram.version;
                } else {
                    loginResult.Version = UnityEngine.Application.version;
                }

                var taskSource = new UniTaskCompletionSource();
                WX.Login(new LoginOption {
                    success = callback => {
                        loginResult.Token = callback.code;
                        loginResult.EnvVersion = envVersion;
                        taskSource.TrySetResult();
                    },
                    fail = callback => {
                        UnityEngine.Debug.LogError(callback.errMsg);
                        taskSource.TrySetResult();
                    }
                });
                await taskSource.Task;
                return loginResult;
            }
        }
    }
}
#endif
